#if TOOLS
using Godot;
using Godot.Collections;
using System.Linq;

namespace Janphe.addons.Avatar
{
    [Tool]
    public class Plugin_CollectAnimation
    {

        internal static bool selected(EditorInterface editor)
        {
            var selected = editor.GetSelection().GetSelectedNodes();
            if (selected.Count != 1)
            {
                GD.Print($"请选择 1 个 {typeof(AnimationPlayer)} 节点，当前选择了 {selected.Count} 个节点。");
                foreach (Node p in selected)
                {
                    GD.Print($"{p.Name} {p.GetType().Name}");
                }
                return false;
            }

            var source = selected[0] as AnimationPlayer;
            if (source == null)
            {
                GD.Print($"请选择 {typeof(AnimationPlayer)} 节点，当前选择了 {selected[0].GetType()} 节点。");
                return false;
            }

            return true;
        }

        // 标动作名 1 = 文件名
        // 标动作名 1 = 源动作名
        // 标动作名 n = 文件名 + 源动作名
        // 标动作名 n = 文件名 + 索引
        public static void ReportAnimation1(EditorInterface editor)
        {
            var player = editor.GetSelection().GetSelectedNodes()[0] as AnimationPlayer;
            if (player == null)
                return;

            var anims = player.GetAnimationList().Select(n => player.GetAnimation(n));
            foreach (var anim in anims)
            {
                var tc = anim.GetTrackCount();
                for (var i = 0; i < tc; ++i)
                {
                    var tp = anim.TrackGetPath(i).ToString();
                    var start = tp.LastIndexOf("/") + 1;
                    var length = tp.Length - start;

                    var np = tp.Substr(start, length);
                    GD.Print($"track {i} {np}");

                    anim.TrackSetPath(i, $"Skeleton:{np}");

                    var tt = anim.TrackGetType(i);

                    if (tt == Animation.TrackType.Transform)
                    {
                        var kc = anim.TrackGetKeyCount(i);
                        anim.TrackGetKeyValue(i, 0);
                    }

                }

            }
        }

        public static void ReportAnimationN(EditorInterface editor)
        {
            if (!Plugin_CollectAnimation.selected(editor))
                return;
            var selected = editor.GetSelection().GetSelectedNodes()[0] as AnimationPlayer;

            //GD.Print($"EditorInterface.GetCurrentPath {editor.GetCurrentPath()}");//file
            GD.Print($"EditorInterface.GetSelectedPath {editor.GetSelectedPath()}");//path

            var current = editor.GetSelectedPath();

            var fs = editor.GetResourceFilesystem();
            var dir = fs.GetFilesystemPath(current);

            var idx = 0;
            for (var i = 0; i < dir.GetFileCount(); ++i)
            {
                var name = dir.GetFile(i);

                if (name.ToLower().EndsWith(".fbx"))
                {
                    var start = name.IndexOf('@') + 1;
                    var aniName = name.Substr(start, name.Length - start - 4);
                    importAnim(selected, idx++, aniName, dir.GetFilePath(i));
                }
            }
        }

        private static void importAnim(AnimationPlayer owner, int idx, string name, string path)
        {
            var scn = GD.Load<PackedScene>(path);
            var tree = scn.Instance();

            var player = tree.GetNode<AnimationPlayer>("AnimationPlayer");
            if (player != null)
            {
                var anims = player.GetAnimationList();
                if (anims.Length == 1)
                    parseAnim(anims[0], 0);
                else
                {
                    for (var i = 0; i < anims.Length; ++i)
                    {
                        parseAnim(anims[i], i + 1);
                    }
                }
            }

            tree.Free();

            void parseAnim(string aName, int aidx)
            {
                var an = aidx == 0 ? name : $"{name}-{aidx}";
                if (owner.HasAnimation(an))
                {
                    GD.Print($"重复动作名称 {idx} {path} {aidx}:{an}");
                    return;
                }

                var anim = player.GetAnimation(aName);

                var tc = anim.GetTrackCount();

                var names = new string[tc];
                for (var i = 0; i < tc; ++i)
                {
                    var tPath = anim.TrackGetPath(i).ToString();
                    var start = tPath.LastIndexOf('/');
                    if (start < 0)
                        start = tPath.LastIndexOf(':');
                    names[i] = tPath.Substring(start + 1);
                }

                var bones = Plugin_RestructSkeleton.MatchBones(names);
                for (var i = 0; i < tc; i++)
                {
                    anim.TrackSetPath(i, $"Skeleton:{bones[i].name}");
                }

                owner.AddAnimation(an, anim);
            }
        }
    }
}
#endif
